<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;
use think\Session;
use app\admin\model\exchange\Order;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    protected $model = null;
    protected $agent_id = -1;
    protected $store_no = -1;
    protected $admin_id = -1;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('AdminLog');
        $user_info = Session::get('admin');//获取当前登录用户信息
        $this->agent_id = $user_info['agent_id'];
        $this->store_no = $user_info['store_no'];
        $this->admin_id = $user_info['id'];
    }

    /**
     * 查看
     */
     public function index()
     {
         $totle_report_pay_key = '';
         $totle_report_pay_val = '';
         $totle_report_num_key = '';
         $totle_report_num_val = '';
         $lingdian = strtotime(date('Y-m-d 00:00:00'));//后面要改成根据配置来获取时间
         //获取当前订单情况
         $totle_pay_arr = [];
         $totle_num_arr = [];
         $totle_pay_wx_arr = [];
         $totle_pay_zfb_arr = [];
         $all_pay_order_tmp = Order::where('is_pay',2)
             ->where('createtime','>',$lingdian)
             ->where('is_delete',0)//is_delete只在店铺中生效
             ->select();
         if($all_pay_order_tmp){
             foreach ($all_pay_order_tmp as $item){
                 $totle_pay_arr[] = $item->total_pay_price;
                 $totle_num_arr[] = 1;
                 if($item->pay_type == 1){
                     $totle_pay_wx_arr[] = $item->total_pay_price;
                 }elseif ($item->pay_type == 2){
                     $totle_pay_zfb_arr[] = $item->total_pay_price;
                 }
             }
         }
         $totle_pay = array_sum($totle_pay_arr);
         $totle_pay_wx = array_sum($totle_pay_wx_arr);
         $totle_pay_zfb = array_sum($totle_pay_zfb_arr);
         $totle_num = count($totle_num_arr);

         //当前用户信息
         $today_user = 0;
         $today_user_tmp = User::where('createtime','>',$lingdian)->select();//今天注册数
         if($today_user_tmp){
             $today_user = count($today_user_tmp);
         }
         $all_user = 0;
         $all_user_tmp = User::where('store_no',$this->store_no)->select();//总注册数
         if($all_user_tmp){
             $all_user = count($all_user_tmp);
         }

         //获取前7天订单汇总报告
         $sen_date = date('Y-m-d',strtotime('-7 day'));
         $totle_report_pay = [];
         $totle_report_num = [];
         $all_report_order_tmp = \app\admin\model\report\Order::where('date','>',$sen_date)->select();
         if($all_report_order_tmp){
             foreach ($all_report_order_tmp as $value){
                 $totle_report_pay_arr[$value->date][] = $value->pay_price;
                 $totle_report_num_arr[$value->date][] = $value->pay_num;

                 $totle_report_pay[$value->date] = array_sum($totle_report_pay_arr[$value->date]);
                 $totle_report_num[$value->date] = array_sum($totle_report_num_arr[$value->date]);
             }
         }
         $totle_report_pay_key = '';
         $totle_report_pay_val = '';
         if($totle_report_pay){
             foreach ($totle_report_pay as $kk=>$vv){
                 $totle_report_pay_key_arr[] = $kk;
                 $totle_report_pay_val_arr[] = $vv;
             }
             $totle_report_pay_key = json_encode($totle_report_pay_key_arr);
             $totle_report_pay_val = json_encode($totle_report_pay_val_arr);
         }
         $totle_report_num_key = '';
         $totle_report_num_val = '';
         if($totle_report_num){
             foreach ($totle_report_num as $kk=>$vv){
                 $totle_report_num_key_arr[] = $kk;
                 $totle_report_num_val_arr[] = $vv;
             }
             $totle_report_num_key = json_encode($totle_report_num_key_arr);
             $totle_report_num_val = json_encode($totle_report_num_val_arr);
         }

//        var_dump($totle_report_pay);
//        var_dump($totle_report_num);die;

         $this->view->assign("total_pay", $totle_pay);
         $this->view->assign("total_num", $totle_num);
         $this->view->assign("total_pay_wx", $totle_pay_wx);
         $this->view->assign("total_pay_zfb", $totle_pay_zfb);
         $this->view->assign("today_user", $today_user);
         $this->view->assign("all_user", $all_user);
         $this->view->assign("totle_report_pay_key", $totle_report_pay_key);
         $this->view->assign("totle_report_pay_val", $totle_report_pay_val);
         $this->view->assign("totle_report_num_key", $totle_report_num_key);
         $this->view->assign("totle_report_num_val", $totle_report_num_val);

         return $this->view->fetch();
     }
     
     
    public function index_bak()
    {
        try {
            \think\Db::execute("SET @@sql_mode='';");
        } catch (\Exception $e) {

        }
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
            ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
            ->group('join_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $userlist = array_fill_keys($column, 0);
        foreach ($joinlist as $k => $v) {
            $userlist[$v['join_date']] = $v['nums'];
        }

        $dbTableList = Db::query("SHOW TABLE STATUS");
        $addonList = get_addon_list();
        $totalworkingaddon = 0;
        $totaladdon = count($addonList);
        foreach ($addonList as $index => $item) {
            if ($item['state']) {
                $totalworkingaddon += 1;
            }
        }
        $this->view->assign([
            'totaluser'         => User::count(),
            'totaladdon'        => $totaladdon,
            'totaladmin'        => Admin::count(),
            'totalcategory'     => \app\common\model\Category::count(),
            'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
            'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
            'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
            'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
            'threednu'          => User::whereTime('jointime', '-3 days')->count(),
            'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
            'dbtablenums'       => count($dbTableList),
            'dbsize'            => array_sum(array_map(function ($item) {
                return $item['Data_length'] + $item['Index_length'];
            }, $dbTableList)),
            'totalworkingaddon' => $totalworkingaddon,
            'attachmentnums'    => Attachment::count(),
            'attachmentsize'    => Attachment::sum('filesize'),
            'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
            'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
        ]);

        $this->assignconfig('column', array_keys($userlist));
        $this->assignconfig('userdata', array_values($userlist));

        return $this->view->fetch();
    }

}
